/* 
 * File:   TessMetaInfo.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/TessMetaInfo.h"

/**
 * Constructor of class TessMetaInfo.
 * 
 * It receives the path to the high resolution
 * binarized table.
 * 
 * The path waited must have the follow format (example):
 * "/books/caracterizacao/metadados/tabelasAlta/image278_0.png"
 * 
 * @param highTablePath: path to high resolution table
 */
TessMetaInfo::TessMetaInfo(string highTablePath) throw (NullImageException) : MetaInfo()  {  
    try { 
        setOriginalPath(highTablePath);
    } 
    catch (NullImageException nie) {
        throw &nie;
    }
    
    setMetadata();
}

TessMetaInfo::TessMetaInfo(const TessMetaInfo& orig) : MetaInfo(orig) {
}

TessMetaInfo::~TessMetaInfo() {
}

/**
 * Returns the tiff image path.
 * 
 * @return tiff image path
 */
string TessMetaInfo::getTIFFImagePath() {
    return this->tiffImagePath;
}

/**
 * Sets the tiff image path.
 * 
 * @param path: path to tiff image
 */
void TessMetaInfo::setTIFFImagePath() {
    this->tiffImagePath = ".." + BOOKS + getBookName() + METADADOS + TABELAS_ALTA +
            getPageName().replace(getPageName().size()-4, getPageName().size(), ".tiff");
}

/**
 * Returns the user answer file path.
 * 
 * @return user answer file path
 */
string TessMetaInfo::getUserAnswerPath() {
    return this->userAnswerPath;
}

/**
 * Sets the user answer file path.
 * 
 * @param path: path to user answer file
 */
void TessMetaInfo::setUserAnswerPath() {
    this->userAnswerPath = ".." + BOOKS + getBookName() + METADADOS + USER_ANSWER + 
            getPageName().replace(getPageName().size()-4, getPageName().size(), ".txt");
}

/**
 * Returns the transcription file path.
 * 
 * @return path to transcription file
 */
string TessMetaInfo::getTranscriptionPath() {
    return this->transcriptionPath;
}

/**
 * Sets the transcription path.
 * 
 * @param path: path to transcription file
 */
void TessMetaInfo::setTranscriptionPath() {
    this->transcriptionPath = ".." + BOOKS + getBookName() + TRANSCRIPTIONS + 
            getPageName().replace(getPageName().size()-4, getPageName().size(), ".txt");
//    printf("transcription path: %s\n", transcriptionPath.c_str());
}

/**
 * Sets the path to high resolution image.
 * 
 * @param imagePath
 */
void TessMetaInfo::setOriginalPath(string highTablePath) throw (NullImageException) {
    if(!(pathValidator->validatePathToTables(pathValidator->HIGH_TABLE, highTablePath))) {
        throw NullImageException(highTablePath);
    }
    else {
        this->originalPath = highTablePath;
    }
}

/**
 * Configures the metadata.
 * 
 */
void TessMetaInfo::setMetadata() {
    setSubMetadata();
    
    setHighResolutionImagePath();
    setLowResolutionImagePath();
    
    setLowResolutionTablePath();
    setHighResolutionTablePath();
    
    setOutputConfidencesFileName();
    setOutputTextsFileName();
    setTIFFImagePath();
    setTranscriptionPath();
    setUserAnswerPath();
}

/**
 * Configures the submetadata that
 * are needs to forms the used metadata.
 * The submetada are: bookName and pageName
 * 
 */
void TessMetaInfo::setSubMetadata() {
    int i = 1, j = 0;
    string aux = "";
    while(j < originalPath.length()-1) {
        if(originalPath.at(j) == '/') {
            if(i == 3) {
                setBookName(aux);
//                printf("book name: %s\n", this->bookName.c_str());
            }
            else if(i == 5) {
                setPageName(originalPath.substr(j, originalPath.length()-j));
//                printf("page name: %s\n", this->pageName.c_str());
            }
            aux = "";
            i++;
        }
        aux += originalPath.at(j++);
    }
}

/**
 * Configures the incomplete path
 * to image cut, this path will be with
 * the index roi. The path is received with the indexROI.
 * 
 */
void TessMetaInfo::setHighResolutionTablePath() {
    this->highResolutionTablePath = ".." + BOOKS + getBookName() + METADADOS + TABELAS_ALTA + 
            getPageName();
}

/**
 * Configures the incomplete path
 * to image cut, this path will be with
 * the index roi, ready that the image is passed with it.
 *  
 */
void TessMetaInfo::setLowResolutionTablePath() {
    this->lowResolutionTablePath = ".." + BOOKS + getBookName() + METADADOS + TABELAS_BAIXA + 
            getPageName();
}

/**
 * Returns the low resolution table path.
 * 
 * @return lowResolutionTablePath
 */
string TessMetaInfo::getLowResolutionTablePath() {
    return this->lowResolutionTablePath;
}

/**
 * Returns the high resolution table path.
 * 
 * @return highResolutionTablePath
 */
string TessMetaInfo::getHighResolutionTablePath() {
    return this->highResolutionTablePath;
}

/**
 * Returns the output texts file name.
 * 
 * @return output texts file name
 */
string TessMetaInfo::getOutputTextsFileName() {
    return outputTextsFileName;
}

/**
 * Sets the output texts file name.
 */
void TessMetaInfo::setOutputTextsFileName() {
    this->outputTextsFileName = ".." + BOOKS + getBookName() + TRANSCRIPTIONS + TEXTS +
                    getPageName().replace(getPageName().size()-4, getPageName().size(), ".txt");
}

/**
 * Returns the output taxes file name.
 * 
 * @return output taxes file name
 */
string TessMetaInfo::getOutputConfidencesFileName() {
    return outputConfidencesFileName;
}

/**
 * Sets the output taxes file name.
 */
void TessMetaInfo::setOutputConfidencesFileName() {
    this->outputConfidencesFileName = ".." + BOOKS + getBookName() + TRANSCRIPTIONS + CONFIDENCES +
                    getPageName().replace(getPageName().size()-4, getPageName().size(), ".txt");
}